package com.liu.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.liu.model.User;
import com.liu.model.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/editInfo")
public class EditInfoServlet extends HttpServlet {
    private ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        resp.setCharacterEncoding("utf8");
        System.out.println(2);
        String email = req.getParameter("email");
        String age = req.getParameter("age");
        String location = req.getParameter("location");
        String profession = req.getParameter("profession");
        String resume = req.getParameter("resume");
        HttpSession session = req.getSession(true);
        User user = (User)session.getAttribute("user");
        int userId = user.getUserId();
        String username = user.getUsername();
        System.out.println(3);
        resp.setContentType("text/html; charset=utf8");

        UserDao userDao = new UserDao();
        try{
            userDao.editInfo(userId, email, age, location, profession, resume);
            resp.getWriter().write(user.getImageUrl());
            User newUser = userDao.selectByName(username);
            session.setAttribute("user", newUser);
            System.out.println(4);
        } catch (Exception e) {
            e.printStackTrace();
            resp.getWriter().write("修改失败！");
        }
    }
}
